<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 25/05/11
 * Time: 15:05
 * To change this template use File | Settings | File Templates.
 */
require_once('./sds/includes/Config.class.php');


class SDS
{

    public function getID(){
        return 15;
    }

    public function getName(){
        return "Second Devision Squad";
    }

    public static function procces()
    {
        require_once(Config::acquire('Register', APPLICATION));
        /**
         *  Setting up a database connection
         */
        require_once(Config::acquire('SQL', APPLICATION));
        SQL::connect('localhost', 'root', '');
        SQL::select_database('ict service');

        /**
         *
         */
        require_once(Config::acquire('Medewerker', MODEL));
        session_start();
        /**
         *  setting up a view
         */
        require_once(Config::acquire('ViewFactory', null, 'view'));
        $view = Viewfactory::get_View(SMARTY);

        /**
         *  decode the url
         */
        require_once(Config::acquire('UrlDecoder', APPLICATION));
        UrlDecoder::decode();

        /**
         * select controller
         */

        if (UrlDecoder::get_Controller_Name() == null) {
            require_once(Config::acquire('Default', CONTROLLER));
            $controller = new  DefaultController($view);
        } else if (!Config::controller_exists(UrlDecoder::get_Controller_Name())) {
            require_once(Config::acquire('Error', CONTROLLER));
            $controller = new  ErrorController($view);
        } else {
            require_once(Config::acquire(UrlDecoder::get_Controller_Name(), CONTROLLER));
            $controllerName = UrlDecoder::get_Controller_Name() . 'Controller';
            $controller = new  $controllerName($view);
        }
        /**
         * selecting action
         */
        if (UrlDecoder::get_Action_Name() == null || !(method_exists($controller, UrlDecoder::get_Action_Name()) == 1)) {
            $action = 'index';
        } else {
            $action = URLDecoder::get_Action_Name();
        }
        /**
         * setting the params.
         */
        $params = UrlDecoder::get_Params();
        /**
         *   executing controller with action and params.
         */

        $controller->$action($params);
        /**
         * disconnect database
         */
        SQL::disconnect();
    }

    public static function redirect($Controller, $action, $params = array())
    {
        header('location: ' . (self::getURL($Controller, $action, $params)));
        return;
    }

    public static function getURL($Controller, $action, $params = array())
    {
        $location = 'http://localhost';
        $location .= SITE_PREFIX;
        if (isset($Controller)) {
            $location .= $Controller . '/';
            if (isset($action)) {
                $location .= $action . '/';
                if (is_array($params) && sizeof($params) >= 1)
                    foreach ($params as $param)
                        $location .= $param . '/';
            }
        }
        return substr($location, 0, (strlen($location) - 1));
    }

}
